//==============================================================================
//
//           Copyright (C) 2002-2005  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2005-02-05 06:25:39 $ $Revision: 1.3 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DFile.h"

#include "DTest.h"


void DFile_test()
{
  DFile *file = [DFile alloc];
  DList *list;
  
  STARTTEST();

  [file init];

  // Test for non-present files
  TEST(![file open :"jdflkasdfklj.dsf" :"r"]);
  TEST([file error] == 2);
  TEST(![file isOpen]);
  
  // Test file writing
  TEST([file open :"test.txt" :"w"]);
  TEST([file isOpen]);
  
  TEST([file writeData    :"Hallo" :3]);
  TEST([file writeByte    :75]);
  TEST([file writeChar    :'a']);
  TEST([file writeDouble  :3.76]);
  TEST([file writeLong    :567890]);
  TEST([file writeShort   :1200]);
  TEST([file writeLine    :"going"]);
  TEST([file writeText    :"gone\n"]);
  
  TEST([file flush]);
  
  [file close];
 
  // Test file reading
  TEST([file open :"test.txt" :"r"]);

  TEST([[file readData :3] bcompare :"Hallo" :3] == 0);
  TEST( [file readByte]   == 75);
  TEST( [file readChar]   == 'a');
  TEST( [file readDouble] == 3.76);
  TEST( [file readLong]   == 567890);
  TEST( [file readShort]  == 1200);
  TEST([[file readLine]  ccompare :"going"] == 0);
  TEST([[file readText]  ccompare :"gone\n"] == 0);

  [file close];

  // Test file and list
  TEST([file open :"test.1" :"r"]);

  list = [file readLines];
  
  TEST([list length] == 15);
  
  [file close];
  
  TEST([file open :"test.2" :"w"]);

  TEST([file writeLines :list]);

  [file close];
   
  [file free];

  // Class methods
  TEST( [DFile isFile      :"test.2"]);
  TEST(![DFile isDirectory :"test.2"]);
  TEST(![DFile isFile      :"."]);
  TEST( [DFile isDirectory :"."]);

  TEST( [DFile move :"test.2" :"moved.3"]);
  TEST(![DFile isFile      :"test.2"]);
  TEST( [DFile isFile      :"moved.3"]);

#ifdef WIN32
  TEST( [DFile size :"moved.3"] == 3019); // Gives two incorrect warnings..
#else
  TEST( [DFile size :"moved.3"] == 3004); // Gives two incorrect warnings..
#endif
  
  //printf("Modified: %s\n", [[[DFile modified :"DFile_test.m"] toText] cstring]); // Well: 2 objects not freed..
  //printf("Accessed: %s\n", [[[DFile accessed :"DFile_test.m"] toText] cstring]);
  
  TEST( [DFile remove :"moved.3"]); // Gives an incorrect warning..
  TEST(![DFile isFile      :"moved.3"]);
  
  STOPTEST();
}

